/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;
import com.naizhuai.ui.entity.management.ChoseUserAdapterBean;
import com.naizhuai.ui.entity.management.DepartmentBean;
import com.naizhuai.util.ConstantsState;
import com.naizhuai.util.ImageUtils;

import java.util.ArrayList;
import java.util.List;

public class DialogCheckedAdapterHolder<T> extends RecyclerView.Adapter {

    private List<ChoseUserAdapterBean<T>> entityLists;
    private OnItemClickListener mItemClickListener;
    private Context mContext;

    public DialogCheckedAdapterHolder(Context context,List<ChoseUserAdapterBean<T>> entityLists) {
        this.mContext = context;
        this.entityLists=entityLists;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent , int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_dialog_chose_muti_people_checked_item, parent, false);
        return new MutiTypeUserCheckedViewHolder<T>(sView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder baseHolder, int position) {
        ChoseUserAdapterBean<T> entity= entityLists.get(position);
        MutiTypeUserCheckedViewHolder holder= (MutiTypeUserCheckedViewHolder) baseHolder;
        if(entity.getEntity() instanceof PersonUserBean){
            PersonUserBean userBean= (PersonUserBean) entity.getEntity();
            holder.tvUserName.setText(userBean.getShowName());
            holder.tvUserNumber.setText(userBean.getPhoneNumber());
            if(entityLists.get(position).getChecked()== ConstantsState.CHECKED)
                holder.checked.setBackgroundResource(R.drawable.dialog_chose_adapter_checked);
            else
                holder.checked.setBackgroundResource(R.drawable.dialog_chose_adapter_unchecked);
            ImageUtils.glideThumbViewByUrl(mContext,holder.mvUserThumb,userBean.getUserLogoUrl());
        }else if(entity.getEntity() instanceof DepartmentBean){
            DepartmentBean partBean= (DepartmentBean) entity.getEntity();
            holder.tvUserName.setText(partBean.getDepartmentName());
            String departmentCount=partBean.getDepartmentCount()+"人";
            holder.tvUserNumber.setText(departmentCount);
            if(entityLists.get(position).getChecked()== ConstantsState.CHECKED)
                holder.checked.setBackgroundResource(R.drawable.dialog_chose_adapter_checked);
            else
                holder.checked.setBackgroundResource(R.drawable.dialog_chose_adapter_unchecked);
            Glide.with(mContext).load(partBean.getDepartmentLogUrl())
                    .placeholder(R.mipmap.virtual_user_image_thumb).error(R.mipmap.virtual_user_image_thumb)
                    .into( holder.mvUserThumb);
        }
       
    }


    @Override
    public int getItemCount() {
        return entityLists.size();
    }

    public class MutiTypeUserCheckedViewHolder<T> extends RecyclerView.ViewHolder{
        TextView tvUserName,tvUserNumber;
        ImageView mvUserThumb,checked;

        public MutiTypeUserCheckedViewHolder(View view) {
            super(view);
            tvUserName = (TextView) view.findViewById(R.id.username);
            tvUserNumber = (TextView) view.findViewById(R.id.user_phone_number);
            mvUserThumb = (ImageView) view.findViewById(R.id.user_thumb);
            checked = (ImageView) view.findViewById(R.id.user_checked_circle);
            view.setOnClickListener(new OddPersonListener(checked));
        }

        class OddPersonListener implements View.OnClickListener{
            private ImageView image;

            public OddPersonListener(ImageView image) {
                this.image = image;
            }

            @Override
            public void onClick(View v) {

                int check=entityLists.get(getAdapterPosition()).getChecked();
                if(check==ConstantsState.CHECKED){
                    entityLists.get(getAdapterPosition()).setChecked(ConstantsState.UNCHECKED);
                    image.setBackgroundResource(R.drawable.dialog_chose_adapter_unchecked);
                }else{
                    entityLists.get(getAdapterPosition()).setChecked(ConstantsState.CHECKED);
                    image.setBackgroundResource(R.drawable.dialog_chose_adapter_checked);
                }
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(v, getAdapterPosition());
                }
            }
        }

    }

    public List<T> getCheckedEntity(){
        List<T> users=new ArrayList<>();
        for (ChoseUserAdapterBean<T> userAdapter:entityLists) {
            if(userAdapter.getChecked()==ConstantsState.CHECKED)
                users.add(userAdapter.getEntity());
        }
        return users;
    }



    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
